package vn.edu.uit.chemistry.utilities;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import vn.edu.uit.chemistry.protocol.BaiCu;
import vn.edu.uit.chemistry.protocol.BaiGiang;
import vn.edu.uit.chemistry.protocol.BatKy;
import vn.edu.uit.chemistry.protocol.CapNhatKiemTra15;
import vn.edu.uit.chemistry.protocol.DapAnKiemTra15;
import vn.edu.uit.chemistry.protocol.KetQuaKiemTra15;
import vn.edu.uit.chemistry.protocol.KichHoatGame;
import vn.edu.uit.chemistry.protocol.KiemTra15;
import vn.edu.uit.chemistry.protocol.Message;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class TCPClient implements Runnable {

	Context ctx;
	Socket sk;
	String host = "";
	int port = 0;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;

	public TCPClient(Context _ctx, String _host, int _port)
			throws UnknownHostException, IOException {
		this.ctx = _ctx;
		this.host = _host;
		this.port = _port;
		sk = new Socket(host, port);
		oos = new ObjectOutputStream(sk.getOutputStream());
		ois = new ObjectInputStream(sk.getInputStream());
	}

	public void stop() throws IOException {
		oos.close();
		ois.close();
		sk.close();
	}

	public void guiBaiKT15(String path) throws IOException,
			ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];

		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();

		oos.writeObject(new KiemTra15(myfile.getName(), "" + myfile.length(),
				tenGV, msgv, ipGV));

		Message fileKT15 = (Message) ois.readObject();

		if (fileKT15 instanceof KiemTra15) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			String value1 = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value1 + 1);
			editor.commit();

			prefs = ctx.getSharedPreferences("DSLamBai", 0);
			editor = prefs.edit();
			editor.putString(host, value + "#0");
			editor.commit();
		}
	}

	public void kichHoatLamKT15() throws IOException {
		oos.writeObject(new CapNhatKiemTra15("", "", "", ""));
	}

	public void guiDapAnKT15(String dapan) throws IOException {
		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();
		oos.writeObject(new KetQuaKiemTra15(tenGV, msgv, ipGV, "", dapan));
	}

	public void guiDapAnChiTietKT15(String path) throws IOException,
			ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];

		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();

		oos.writeObject(new DapAnKiemTra15(myfile.getName(), ""
				+ myfile.length(), tenGV, msgv, ipGV));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof DapAnKiemTra15) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiTaiLieu(String path) throws IOException,
			ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];

		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();

		oos.writeObject(new BaiGiang(myfile.getName(), "" + myfile.length(),
				tenGV, msgv, ipGV));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BaiGiang) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiDapAnBaiCu(String path) throws IOException,
			ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];

		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();

		oos.writeObject(new BaiCu(myfile.getName(), "" + myfile.length(),
				tenGV, msgv, ipGV));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BaiCu) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();
			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void guiFileBatKy(String path) throws IOException,
			ClassNotFoundException {
		File myfile = new File(path);
		byte[] myByteArray = new byte[(int) myfile.length()];

		SharedPreferences prefs = ctx.getSharedPreferences("TaiKhoanGV", 0);
		String tenGV = prefs.getString("hoten", "").toString();
		String msgv = prefs.getString("msgv", "").toString();
		String ipGV = prefs.getString("ip", "").toString();

		oos.writeObject(new BatKy(myfile.getName(), "" + myfile.length(),
				tenGV, msgv, ipGV));

		Message fileBaiGiang = (Message) ois.readObject();

		if (fileBaiGiang instanceof BatKy) {

			Socket socket = new Socket(host, 9999);
			FileInputStream fis = new FileInputStream(myfile);
			BufferedInputStream bis = new BufferedInputStream(fis);
			bis.read(myByteArray, 0, myByteArray.length);
			OutputStream os = socket.getOutputStream();
			System.out.println("Sending...");
			os.write(myByteArray, 0, myByteArray.length);
			os.flush();

			System.out.println("Finish...");
			socket.shutdownOutput();

			prefs = ctx.getSharedPreferences("DSGui", 0);
			String value = prefs.getString(host, "mshs#tenHS#0");
			value = value.substring(0, value.length() - 1);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString(host, value + 1);
			editor.commit();
		}
	}

	public void kichHoatGame() throws IOException {
		oos.writeObject(new KichHoatGame());
	}
	
	public void run() {
		//
	}

}
